// pages/mine/mine.js
import Dialog from '@vant/weapp/dialog/dialog';
var util = require('../../utils/util')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    token: app.globalData.token || wx.getStorageSync('token'),
    vipInfo: {level: '0', develop: 0},
    level: '',
    vipLevleList: [], // 会员信息列表
    isSign: false, // 是否签到
    shareBanner: '', // 邀请banner
    notPay: 0, // 待支付
    notReceipt: 0, // 未收货
    notShipping: 0, // 未发货
    notComment: 0, // 未评价
    notReadCount: 0, // 未读消息
    groupSrc: app.globalData.host,
    tab_list: [{
        title: '购物车',
        icon: '/common/icons/mine/tab_icon6.png',
        pageUrl: '/pages/shopCar/shopCar'
      },
      {
        title: '待付款',
        icon: '/common/icons/mine/tab_icon1.png',
        pageUrl: '/pages/orderList/orderList?status=0&selectflag=1'
      },
      {
        title: '待发补款',
        icon: '/common/icons/mine/tab_icon2.png',
        pageUrl: '/pages/packageA/orderList/orderList?status=2&selectflag=2'
      },
      {
        title: '待收货',
        icon: '/common/icons/mine/tab_icon3.png',
        pageUrl: '/pages/orderList/orderList?status=2&selectflag=3'
      },
      {
        title: '待评分',
        icon: '/common/icons/mine/tab_icon0.png',
        pageUrl: '/pages/shopOrderList/orderList?status=3&selectflag=4'
      },
      // {
      //   title: '售后',
      //   icon: '/common/icons/mine/tab_icon4.png',
      //   pageUrl: '/pages/drawBack/drawBack'
      // },
    ],
    cell_list: [{
        title: '发起众筹',
        icon: '/common/icons/mine/list_icon0.png',
        pageUrl: '/pages/wenjuan/wenjuan'
      },
      {
        title: '收货地址',
        icon: '/common/icons/mine/list_icon1.png',
        pageUrl: '/pages/addressList/addressList'
      },
      {
        title: '我的分享',
        icon: '/common/icons/mine/list_icon5.png',
        pageUrl: '/pages/wallet/wallet'
      },
      {
        title: '我的消息',
        icon: '/common/icons/mine/list_icon7.png',
        pageUrl: '/pages/message/message'
      },
      {
        title: '我的看好',
        icon: '/common/icons/mine/list_icon6.png',
        pageUrl: '/pages/good/good'
      },
      {
        title: '联系客服',
        icon: '/common/icons/mine/list_icon2.png',
        pageUrl: 'custom'
      },
      {
        title: '关于我们',
        icon: '/common/icons/mine/list_icon3.png',
        pageUrl: 'about'
      },
      {
        title: '退出登录',
        icon: '/common/icons/mine/list_icon4.png',
        pageUrl: 'withdraw'
      }
    ]
  },

  // 签到
  sign() {
    util.throttle(()=> {
      wx.post(wx.api.sign, {}, res=> {
        console.info('=== sign ====', res)
        console.info(this)
        if (res.code == 200) {
          wx.showToast({
            title: '签到成功',
            icon: 'none',
            duration: 1000
          })
          this.setData({
            isSign: true
          })
          this.getVipInfo()
        }
      })
    }, 2000)()
  },

  // 查询积分
  getVipInfo() {
    wx.get(wx.api.vipUserInfo, res=> {
      console.info('=== vipUserInfo ====', res)
      if (res.code == 200) {
        this.setData({
          vipInfo: res.data,
          level: res.data.level
        })
        app.globalData.vipInfo = res.data
        wx.setStorage({
          key:"vipInfo",
          data: res.data
        })
      }
    })
  },

  currentChange(e) {
    console.info('==== banner下标 ====', e)
    this.setData({
      level: e.detail.current
    })
  },

  // 会员等级信息
  getvipLevleList() {
    wx.get(wx.api.vipLevleList, res=> {
      console.info('=== vipLevleList ====', res)
      if (res.code == 200) {
        this.setData({
          vipLevleList: res.data
        })
      }
    })
  },

  // 跳转登录
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  // 跳转订单列表
  toOrderList() {
    wx.navigateTo({
      url: '/pages/orderList/orderList',
    })
  },

  // 跳转优惠券列表
  toCoupon() {
    wx.navigateTo({
      url: '/pages/coupon/coupon',
    })
  },

  // 跳转积分明细
  toJifen() {
    wx.navigateTo({
      url: '/pages/jifen/jifen?develop=' + this.data.vipInfo.develop,
    })
  },

  // 申请成为大团长
  toGroup() {
    let url = encodeURIComponent(app.globalData.host + `/doc/groupon.html?token=${this.data.token}`)
    wx.navigateTo({
      url: `/pages/webview/webview?url= ${url}`,
    })
  },
  
  // 跳转页面
  jumpPage(e) {
    if (e.currentTarget.dataset.item.pageUrl == 'withdraw') {
        // 退出登录 清空缓存
        let that = this
        Dialog.confirm({
          title: '温馨提示',
          message: '是否退出登录',
        })
          .then(() => {
            // on confirm
            app.globalData.userInfo = {}
            app.globalData.token = ''
            that.setData({
                userInfo: {},
                vipInfo: {level: '0', develop: 0},
                banner: '',
                notPay: 0,
                notReceipt: 0,
                notShipping: 0,
                notComment: 0,
                notReadCount: 0
            })
            wx.removeStorage({
              key: 'vipInfo'
          })
            wx.removeStorage({
                key: 'userInfo'
            })
            wx.removeStorage({
                key: 'token'
            })
          })
          .catch(() => {
            // on cancel
          });
    } else if (e.currentTarget.dataset.item.pageUrl == 'about') {
      let url = encodeURIComponent(app.globalData.host+'/doc/aboutAs.html')
      wx.navigateTo({
        url: `/pages/webview/webview?url= ${url}`,
      })
    } else {
        if(!this.data.userInfo.nickName) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
            return
        }
        wx.navigateTo({
            url: e.currentTarget.dataset.item.pageUrl
        })
    }
  },

  // 跳转分享
  toShare() {
    wx.navigateTo({
      url: "/pages/share/share",
    })
  },

  toUserInfo() {
    wx.navigateTo({
      url: "/pages/getUserInfo/getUserInfo",
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { 
    this.getvipLevleList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo || wx.getStorageSync('userInfo'),
      vipInfo: app.globalData.vipInfo || wx.getStorageSync('vipInfo'),
    })
    wx.get(wx.api.isLogin, res=> {
      console.info('=== isLogin ====', res)
      if (res.code == 200) {
        this.getVipInfo()
        this.getUserMsg()
      }
    })
  },
  getUserMsg() {
    wx.get(wx.api.userMessage, res=> {
      console.info('=== userMessage ====', res)
      if (res.code == 200) {
        this.setData({
          notPay: res.data.notPay, // 待支付
          notReceipt: res.data.notReceipt, // 未收货
          notShipping: res.data.notShipping, // 未发货
          notComment: res.data.notComment,  // 未评价
          notReadCount: res.data.notReadMessage, // 未读消息
          isSign: res.data.isSign, // 是否签到
          shareBanner: res.data.inviteBanner, // 分享banner
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})